<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索区域 -->
      <div class="search-area">
        <el-row :gutter="20">
          <!-- 搜索框 -->
          <el-col class="col-item" :span="8">
            <el-input placeholder="请输入内容" clearable v-model="query" class="input-with-select">
              <el-button @click="searchItem" slot="append" icon="el-icon-search"></el-button>
            </el-input>
          </el-col>
          <!-- 添加用户 -->
          <el-col class="col-item" :span="5">
            <el-button type="primary" @click="goadd">添加商品</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 表格区域 -->
      <el-table :data="goodsList" border>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column width="350px" label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格（元）" prop="goods_price"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="商品重量" prop="add_time">
          <template slot-scope="scope">
            {{scope.row.add_time|dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
            <el-button @click="removeGoods(scope.row.goods_id)" size="mini" type="danger" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import{getgoodslist,delgoods} from '@/api/login'
export default {
  data(){
    return{
      query:"",
      pagenum:1,
      pagesize:5,
      total:0,
      // 商品数组
      goodsList:[]
    }
  },
  created(){
    this.loadDate()
  },
  methods:{
    loadDate(){
      const{query,pagenum,pagesize}=this
      getgoodslist({query,pagenum,pagesize}).then(({data})=>{
        console.log(data)
        this.goodsList=data.data.goods
        this.total=data.data.total
      })
    },
    // 根据关键字搜索商品
    searchItem(){
      this.loadDate()
    },
    // 展示数量监听事件
    handleSizeChange(size){
      console.log(size)
      this.pagesize=size
      // 参数发生改变，重新获取数据
      this.loadDate()
    },
    // 页码监听事件
    handleCurrentChange(num){
      console.log(num)
      // 将获取到的页面数同步到data
      this.pagenum=num
      // 页码数发生改变，重新获取数据
      this.loadDate()
    },
    // 页面跳转（商品添加页面）
    goadd(){
      this.$router.push('/goods/Add')
    },
    // 删除商品
    async removeGoods(Id){
      const{$api:{Success,Error},goodsList}=this
      const res= await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).catch(err => err)
      console.log(res)
      if(res=="confirm"){
        for(let i in goodsList){
          if(goodsList[i].goods_id==Id){
            console.log(i,Id)
            goodsList.splice(i,1)
          }
        }
        delgoods('goods/'+Id+'').then(({data})=>{
          console.log(data)
          if(data.meta.status==200){
            Success(data.meta.msg)
          }
        })
      }else{
        Error("取消删除")
      }
      
    }
  }
}
</script>
<style Lang="less" scoped>
.col-item{
  display: flex;
}
</style>